package com.sojson.project.pay.service.wx.impl.tests;

import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import com.ijpay.wxpay.WxPayApi;
import com.sojson.project.sys.config.entity.vo.ConfigVo;
import com.sojson.util.SystemParamUtil;
import com.sojson.util.encrypt.enums.EEncryptType;
import com.sojson.util.json.JsonUtil;
import com.sojson.util.pay.wx.PayWxParamNofityVerifyInterface;
import com.sojson.util.pay.wx.WxUtil;
import com.sojson.util.pay.wx.bean.WxResponseV2;
import com.sojson.util.pay.wx.bean.WxResponseV3;
import com.sojson.util.pay.wx.constant.WxConstant;
import com.sojson.util.servlet.ServletUtil;
import com.sojson.util.xml.XmlUtil;

/**
 * 微信支付回调参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
@Component
/** 是否注入这个Bean */
@Conditional(TestConditionPayWx.class)
public class PayWxParamNofityVerifyTest implements PayWxParamNofityVerifyInterface {

    /**
     * 支付回调
     * 
     * @param request 
     * @return
     * @throws Exception 
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public WxResponseV3 payNofity(HttpServletRequest request) throws Exception {
        // 从请求中获取参数
        TreeMap<String, String> treeMap = new TreeMap<>(XmlUtil.xmlToMap(ServletUtil.getParamBody()));

        // 参数验证
        Map<String, ConfigVo> config = SystemParamUtil.gets();
        String mchid = config.get(WxConstant.MCHID_CACHE_NAME).getVal();
        String mchKey = config.get(WxConstant.APIKEY_CACHE_NAME).getVal();
        String signKey = WxPayApi.getSignKey(mchid, mchKey, null);
        Map<String, String> xmlToMap = XmlUtil.xmlToMap(signKey);
        mchKey = xmlToMap.get(WxConstant.APIKEY_SANDBOX_SIGNKEY_CACHE_NAME);

        if (!WxUtil.paySignIsSuccessObject((TreeMap)treeMap, mchKey, EEncryptType.MD5)) {
            return null;
        }

        WxResponseV3 jsonToObject = JsonUtil.jsonToObject(JsonUtil.objectToJson(treeMap), WxResponseV3.class);
        jsonToObject.setTradeState(treeMap.get(WxConstant.FIELD_RESULT_CODE).toString());
        jsonToObject.setRefundStatus(treeMap.get(WxConstant.FIELD_RESULT_CODE).toString());
        return jsonToObject;
    }

    /**
     * 支付回调(V2接口)
     * 
     * @param request 
     * @return
     * @throws Exception 
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public WxResponseV2 payNofityV2(HttpServletRequest request) throws Exception {
        // 从请求中获取参数
        TreeMap<String, String> treeMap = new TreeMap<>(XmlUtil.xmlToMap(ServletUtil.getParamBody()));

        // 参数验证
        String mchKey = SystemParamUtil.getStr(WxConstant.APIKEY_CACHE_NAME);
        if (!WxUtil.paySignIsSuccessObject((TreeMap)treeMap, mchKey)) {
            return null;
        }

        return JsonUtil.jsonToObject(JsonUtil.objectToJson(treeMap), WxResponseV2.class);
    }

    /**
     * 退款回调(V2接口)
     * 
     * @param request 
     * @return
     * @throws Exception 
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public WxResponseV2 payNofityCancelV2(HttpServletRequest request) throws Exception {
        // 从请求中获取参数
        TreeMap<String, String> treeMap = new TreeMap<>(XmlUtil.xmlToMap(ServletUtil.getParamBody()));

        // 参数验证
        String mchKey = SystemParamUtil.getStr(WxConstant.APIKEY_CACHE_NAME);
        if (!WxUtil.paySignIsSuccessObject((TreeMap)treeMap, mchKey)) {
            return null;
        }

        WxResponseV2 jsonToObject = JsonUtil.jsonToObject(JsonUtil.objectToJson(treeMap), WxResponseV2.class);
        return jsonToObject;
    }

}